
package edu.tfg.dao;

import edu.tfg.model.Endereco;
import edu.tfg.util.HibernateUtil;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

/**
 *
 * @author Juarez
 */
public class EnderecoDAO {
    
    private Session sessao;
    private Transaction trans;
    private List <Endereco> lista;
    
       
    public void salvar(Endereco end){
        try{
        sessao =  HibernateUtil.getSessionFactory().openSession();
        trans = sessao.beginTransaction();
      
        sessao.save(end);
        trans.commit();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
               sessao.close();
        }
   }
    
    public void removeEndereco(Endereco end){
       try{
        sessao =  HibernateUtil.getSessionFactory().openSession();
        trans = sessao.beginTransaction();
        
        sessao.delete(end);
        trans.commit();
       }catch(Exception e){
           e.printStackTrace();
       }finally{
           sessao.close();
       }      
      
    }
    
   public List<Endereco> listarEndereco() {
	Criteria lista = sessao.createCriteria(Endereco.class);
	return lista.list();
   }
   
   public void alterarEndereco(Endereco cliente) {
	this.sessao.update(cliente);		
   }
   
   public Endereco pesquisarEndereco(String string) {		
	Query consultaNome = this.sessao.createQuery("from Cliente c where c.nome like :nome");
	consultaNome.setString("nome", "%" + string + "%");
	return (Endereco) consultaNome.uniqueResult();		
    }

    
}
